package com.cbw.blog.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cbw.blog.ResponseData.ArticleResponseData;
import com.cbw.blog.ResponseData.StaticticsBo;
import com.cbw.blog.entity.Article;
import com.cbw.blog.entity.Comment;
import com.cbw.blog.service.IArticleService;
import com.cbw.blog.service.ISiteService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

@Slf4j
@Controller
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    ISiteService siteService;
    @Autowired
    private IArticleService articleService;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @GetMapping(value = {"","/index"})
    public String index(HttpServletRequest request){
        Page<Article> articles = siteService.recentArticles(5);

        Page<Comment> comments = siteService.recentComments(5);
        StaticticsBo statistics = siteService.getStatistics();

        request.setAttribute("comments",comments.getRecords());
        request.setAttribute("articles",articles.getRecords());
        request.setAttribute("statistics",statistics);
        return "back/index";
    }


    @GetMapping("/article/toEditPage")
    public String newArticle(){
        return "back/article_edit";
    }

    @PostMapping("article/publish")
    @ResponseBody
    public ArticleResponseData pulishArticle(Article article){
        if (StringUtils.isBlank(article.getCategories())){
            article.setCategories("默认分类");
        }
        try {

            article.setCreated(new Date());
            articleService.save(article);
            log.info("文章发表成功");
            return ArticleResponseData.ok();
        }catch (Exception e){
            log.info("文章发表失败，错误信息{}",e.getMessage());
            return ArticleResponseData.fail();
        }
    }





    @GetMapping("/article")
    public String index(@RequestParam(value = "page",defaultValue = "1")int page,
                        @RequestParam(value = "count",defaultValue = "10")int count,
                        HttpServletRequest request){

        Page<Article> articlePage = articleService.page(new Page<>(page, count), null);
        request.setAttribute("articles",articlePage);
        return "back/article_list";
    }

    @GetMapping("/article/{id}")
    public String editArticle(@PathVariable("id") String id,
                              HttpServletRequest request){

        Article article = articleService.getById(id);
        request.setAttribute("contents",article);
        request.setAttribute("categories",article.getCategories());
        return "back/article_edit";
    }
    @PostMapping ("/article/modify")
    @ResponseBody
    public ArticleResponseData modifyArticle(Article article){
        try {
            articleService.updateArticleWithId(article);
            log.info("文章更新成功");
            return ArticleResponseData.ok();
        }catch (Exception e){
            log.info("文章更新失败，错误信息{}",e.getMessage());
            return ArticleResponseData.fail();
        }
    }

    @PostMapping ("/article/delete")
    @ResponseBody
    public ArticleResponseData deleteArticle(@RequestParam Integer id){
        try {
            articleService.removeById(id);
            redisTemplate.delete("article_"+id);
            log.info("文章删除成功");
            return ArticleResponseData.ok();
        }catch (Exception e){
            log.info("文章删除失败，错误信息{}",e.getMessage());
            return ArticleResponseData.fail();
        }
    }




}
